package com.xuebusi.common.util;

import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class FileUtil {

    /**
     * 生成图片key
     * @Author shiyanjun
     * @Date 2018/9/23 下午8:31
     * @Param
     * @Return
     * @Exception
     */
    public static String genFileKey() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 修改图片名称
     * @Author shiyanjun
     * @Date 2018/9/23 下午8:36
     * @Param
     * @Return
     * @Exception
     */
    public static String getNewName(MultipartFile file, String key) {
        if (file != null) {
            String originalFilename = file.getOriginalFilename();
            if (!StringUtils.isEmpty(originalFilename)) {
                String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
                return key + fileType;
            }
        }
        return null;
    }

    /**
     * 获取文件存储目录
     * @Author shiyanjun
     * @Date 2018/9/23 下午8:45
     * @Param
     * @Return
     * @Exception
     */
    public static String getFileDir() {
        String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String fileDir = "src/main/resources/static/img/" + dateStr + "/";
        return fileDir;
    }

}
